package com.shop.shoporder.wapController;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.shop.shoporder.common.*;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.service.AlipayService;
import com.shop.shoporder.service.OrderService;
import com.shop.shoporder.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.print.DocFlavor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
@RequestMapping("/wap")
@CrossOrigin(origins = "*",maxAge = 3600)
public class PayOrderController {
    @Autowired
    PayOrderService payOrderService;
@Autowired
    OrderService orderService;
@Autowired
    AlipayService alipayService;
//     @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/getPayOrderData",method = RequestMethod.POST)
    //订单支付页面
    public Response getPayOrderData(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Response response=new Response();
        try{
            response=payOrderService.getPayOrderData(map,request);
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"获取异常！",request);
        }
    }

    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/payInter",method = RequestMethod.POST)
    //订单支付页面
    public Response payInter(@RequestParam Map<String,String> map, HttpServletRequest request) {
        Response response=new Response();
        try{
            response=payOrderService.payInter(map,request);
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"系统异常！",request);
        }
    }

    //@UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/payAlipay",method = RequestMethod.GET)
    //订单支付页面
    public Response payAlipay(@RequestParam Map<String,String> map, HttpServletRequest request,HttpServletResponse response) {
        Response responseRe=new Response();
        try{
            responseRe=alipayService.payAlipay(map,request,response);
            return responseRe;
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"系统异常！",request);
        }
    }

    //同步
    @ResponseBody
    @RequestMapping(value = "/alipayReturn",method = RequestMethod.GET)
    public Response alipayReturn(@RequestParam Map<String,String> map, HttpServletRequest request, HttpServletResponse response){
        Response responseRe=new Response();
        try{
            responseRe=alipayService.alipayReturn(map,request,response);
            return responseRe;
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"系统异常！",request);
        }
    }

    //异步
    @ResponseBody
    @RequestMapping(value = "/alipayNotify",method = RequestMethod.GET)
    public Response alipayNotify(@RequestParam Map<String,String> map, HttpServletRequest request, HttpServletResponse response){
        Response responseRe=new Response();
        try{
            responseRe=alipayService.alipayNotify(map,request,response);
            return responseRe;
        }catch (Exception e){
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR,null,0,"系统异常！",request);
        }
    }
}
